package com.wms.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.dto.ConfirmCorrectCountDto;
import com.wms.dto.ConfirmLossCountDto;
import com.wms.dto.StocktakingInfoDto;
import com.wms.mapper.*;
import com.wms.pojo.*;
import com.wms.service.StocktakingInfoService;
import com.wms.vo.StocktakingInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class StocktakingInfoServiceImpl extends ServiceImpl<StocktakingInfoMapper,TbStocktakingInfo> implements StocktakingInfoService {

    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private WarehouseAreaMapper areaMapper;
    @Autowired
    private WarehouseAreaLocalMapper localMapper;
    @Autowired
    private ItemSkuMapper skuMapper;
    @Autowired
    private StocktakingLossMapper lossMapper;

    @Override
    public PageResult queryPage(Integer page, Integer pageSize, Integer id) {
        PageHelper.startPage(page, pageSize);
        List<StocktakingInfoVo> list = getBaseMapper().queryPage(id);
        Page<StocktakingInfoVo> pageInfo = (Page<StocktakingInfoVo>) list;
        return new PageResult(pageInfo.getTotal(), pageInfo.getResult());
    }

    @Override
    public void saveStocktaking(StocktakingInfoDto dto) {
        WarehouseAreaLocal warehouseAreaLocal = localMapper.selectById(dto.getTargetWarehouseAreaLocalCode());
        ItemSku sku = skuMapper.selectOne(Wrappers.<ItemSku>lambdaQuery().eq(ItemSku::getCode, dto.getSkuCode())
                .eq(ItemSku::getTitle, dto.getSkuName())
                .eq(ItemSku::getPrice, dto.getSkuPrice()));
        TbStocktakingInfo stocktakingInfo = TbStocktakingInfo.builder().bookCount(dto.getBookCount())
                .skuId(sku.getId())
                .status(1)
                .targetWarehouseAreaLocaId(warehouseAreaLocal.getId())
                .targetWarehouseAreaId(warehouseAreaLocal.getWarehouseAreaId())
                .targetWarehouseId(warehouseAreaLocal.getWarehouseId())
                .empId(1)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .category(1)
                .createBy("liuyan")
                .build();
        getBaseMapper().insert(stocktakingInfo);
    }

    @Override
    public StocktakingInfoDto queryStocktakingById(Integer id) {
        TbStocktakingInfo tbStocktakingInfo = getBaseMapper().selectById(id);
        ItemSku sku = skuMapper.selectById(tbStocktakingInfo.getSkuId());
        WarehouseAreaLocal warehouseAreaLocal = localMapper.selectById(tbStocktakingInfo.getTargetWarehouseAreaLocaId());
        WarehouseArea warehouseArea = areaMapper.selectById(warehouseAreaLocal.getWarehouseAreaId());
        Warehouse warehouse = warehouseMapper.selectById(warehouseAreaLocal.getWarehouseId());
        StocktakingInfoDto infoDto = StocktakingInfoDto.builder()
                .bookCount(tbStocktakingInfo.getBookCount())
                .skuCode(sku.getCode())
                .skuName(sku.getTitle())
                .skuPrice(sku.getPrice())
                .correctTime(tbStocktakingInfo.getCreateTime().toLocalDate())
                .targetWarehouseName(warehouse.getName())
                .targetWarehouseAreaName(warehouseArea.getName())
                .targetWarehouseAreaLocalCode(tbStocktakingInfo.getTargetWarehouseAreaLocaId())
                .build();
        return infoDto;
    }

    @Override
    public void updateCorrectCountById(ConfirmCorrectCountDto dto) {
        TbStocktakingInfo tbStocktakingInfo = new TbStocktakingInfo();
        tbStocktakingInfo.setId(dto.getId());
        tbStocktakingInfo.setStatus(2);
        //两个相等，说明数量是正确的
        if (Objects.equals(dto.getCorrectCount(), dto.getBookCount())){
            tbStocktakingInfo.setCorrectCount(dto.getCorrectCount());
            //就没有差异数量
            tbStocktakingInfo.setDiscrepancyCount(0);
        }else {
            if (dto.getBookCount()> dto.getCorrectCount()){
                tbStocktakingInfo.setCorrectCount(dto.getCorrectCount());
                tbStocktakingInfo.setDiscrepancyCount(dto.getCorrectCount()- dto.getBookCount());
            }else {
                tbStocktakingInfo.setCorrectCount(dto.getCorrectCount());
                tbStocktakingInfo.setDiscrepancyCount(dto.getBookCount()-dto.getCorrectCount());
            }
        }
        getBaseMapper().updateById(tbStocktakingInfo);
    }

    @Override
    public void updateLossCount(ConfirmLossCountDto dto) {
        TbStocktakingLoss stocktakingLoss = TbStocktakingLoss.builder()
                .lossCount(dto.getLossCount())
                .lossCategory(dto.getLossCategory())
                .stocktakingInfo(dto.getId())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        lossMapper.insert(stocktakingLoss);
    }
}
